package com.teacher.hong.controller;

import com.alibaba.fastjson.JSONObject;
import com.teacher.hong.entity.Student;
import com.teacher.hong.entity.User;
import com.teacher.hong.service.IUserService;
import com.teacher.hong.utils.SecurityUtils;
import com.teacher.hong.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.xml.bind.ValidationException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class IndexController extends BaseController {

    /**
     * 登录（使用req来获取数据）
     * @return
     */
    /*
    @PostMapping("login")
    public Object login(HttpServletRequest req, HttpServletResponse resp) {

        //账号
        String account = req.getParameter("account");
        //密码
        String password = SecurityUtils.md5Encrypt(req.getParameter("password"));

        log.info("接收到的account:" + account);
        log.info("接收到的password:" + password);

        JSONObject object = new JSONObject(true);
        object.put("state", 200);
        object.put("message", "login success");
        object.put("timestamp", LocalDateTime.now());

        return object.toJSONString();
    }
     */

    @Resource
    IUserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("login")
    public Object login(@Valid @RequestBody User user, BindingResult result) throws ValidationException {
        //密码
        user.setPassword(SecurityUtils.md5Encrypt(user.getPassword()));
        userService.login(user);
        JSONObject value = new JSONObject();
        Date issuedTime = new Date();
        Date expiresTime = new Date(issuedTime.getTime() + 1000 * 60 * 60 * 72);  //过期时间为72个小时
        value.put("token", TokenUtils.generate(user.getAccount(), issuedTime, expiresTime));
        return success("登录成功", value);
    }


    /**
     * 退出
     * @return
     */
    @GetMapping("logout/{count}/{name}")
    public Object logout(@PathVariable Integer count, @PathVariable String name) {

        log.info("执行到logout方法了...");
        log.info(request.getMethod());
        log.info("接收到的count:" + count);
        log.info("接收到的name:" + name);

        JSONObject object = new JSONObject(true);
        object.put("state", 200);
        object.put("message", "logout success");
        object.put("timestamp", LocalDateTime.now());

        return object.toJSONString();
    }

    /**
     * 注册（使用request和@RequestParam接收参数）
     * @return
     */
    /*
    @PostMapping("register")
    public Object register(@RequestParam String account, String password, String email) {
        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        user.setEmail(email);
        user.setRealname(request.getParameter("realname"));
        user.setMobile(request.getParameter("mobile"));
        user.setSex(request.getParameter("sex"));
        user.setBirthday(new Date(request.getParameter("birthday").replace("-","/")));   //1990-10-04

        log.info("接收到的user参数：" + user);


        JSONObject object = new JSONObject(true);
        object.put("state", 200);
        object.put("message", "register success");
        object.put("timestamp", LocalDateTime.now());

        return object.toJSONString();
    }
     */

    /**
     * 注册（使用@ModelAttribute）
     * @param student
     * @param user
     * @param account
     * @return
     */
    @PostMapping("register")
    public Object register(@ModelAttribute Student student,
                           @ModelAttribute User user,
                           @RequestParam String account) {
        log.info("接收到的student参数：" + student);
        log.info("接收到的user参数：" + user);
        log.info("接收到的account参数：" + account);

        JSONObject object = new JSONObject(true);
        object.put("state", 200);
        object.put("message", "register success");
        object.put("timestamp", LocalDateTime.now());

        return object.toJSONString();
    }


    /**
     * 注册（使用@RequestBody）
     * @param user
     * @return
     */
    @PostMapping("registerJson")
    public Object registerJson(User user) {

        log.info("接收到的user参数：" + user);

        JSONObject object = new JSONObject(true);
        object.put("state", 200);
        object.put("message", "register success");
        object.put("timestamp", LocalDateTime.now());

        return object.toJSONString();
    }


    @PostMapping("add")
    public Object add(@RequestBody String content) {

        log.info("接收到的content参数：" + content);

        JSONObject object = new JSONObject(true);
        object.put("state", 200);
        object.put("message", "add success");
        object.put("timestamp", LocalDateTime.now());

        return object.toJSONString();
    }
}
